//
//  ZXIconDiscountView.swift
//  
//
//  Created by zhangxing on 2017/8/13.
//
//

import UIKit
import SDWebImage
class ZXIconDiscountView: UIView {

    var iconModelList: [ZXIconModel]? {
        
        didSet {
            
            //遍历模型数组
            for (i, model) in (iconModelList?.enumerated())! {
                
                btnList[i].sd_setImage(with: URL.init(string: model.img!), for: .normal, placeholderImage: #imageLiteral(resourceName: "order_yellowMark"))
                
                btnList[i].sd_setImage(with: URL.init(string: model.img!), for: .highlighted, placeholderImage: #imageLiteral(resourceName: "order_grayMark"))
                
                btnList[i].setTitle(model.name, for: .normal)
                
                btnList[i].setTitle(model.name, for: .highlighted)
            }
        }
        
    }
    
    //创建一个button的可变数组(用于存储创建的button,方便当我们接受到iconModelList时,我们为按钮赋值)
    var btnList = [ZXIconDiscountButton]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  MARK: --   搭建UI
    private func setupUI() {
        
        backgroundColor = UIColor.white
        
        //计算按钮的宽度
        let btnDistance: CGFloat = 25
        let btnW = (KWidth - 5 * btnDistance) / 4
        
        //循环创建按钮
        for i in 0..<4 {
            
            let btn = ZXIconDiscountButton()
            
            //设置属性
            btn.setTitle("敬请期待", for: .normal)
            btn.setTitle("敬请期待", for: .highlighted)
            
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.setTitleColor(UIColor.black, for: .highlighted)
            
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            
            btn.titleLabel?.sizeToFit()
            
            btn.titleLabel?.textAlignment = .center
            
            btn.setImage(#imageLiteral(resourceName: "order_yellowMark"), for: .normal)
            btn.setImage(#imageLiteral(resourceName: "order_yellowMark"), for: .highlighted)
            
            //设置btn的frame
            btn.frame = CGRect(x: btnDistance * CGFloat(i + 1) + btnW * CGFloat(i), y: 0, width: btnW, height: btnW)
            
            //取消点击高亮
            btn.adjustsImageWhenHighlighted = false
            
            //添加btn
            addSubview(btn)
            
            //添加到数组(方便我们接收到iconModelList数组后对这个button重新进行设置值)
            btnList.append(btn)
        }
        
        
        
        
        
        
        
        
    }
}
